package com.weblog.weblog.jwt.handler;

import com.weblog.weblog.common.enums.ResponseCodeEnum;
import com.weblog.weblog.common.utils.Response;
import com.weblog.weblog.jwt.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录成功访问受保护资源，权限不够
 * @author gc
 * @class_name RestAccessDeniedHandler
 * @date 2023年09月15日 21:56
 */
@Slf4j
@Component
public class RestAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
        log.warn("登录成功访问受保护资源，权限不够", accessDeniedException);
        ResultUtils.fail(response, Response.fail(ResponseCodeEnum.FORBIDDEN));
    }

}
